<template>
  <div class="view-fill">
    <div v-if="!modifyInfo.visible" class="app-container-flex">
      <NewFullScreen
        v-model="showDialog"
        title="其他入库模板明细"
        @returnBack="returnBack"
      >
        <template v-slot:full-button>
          <el-button
            v-if="$permission('otherTemplate:update')"
            @click="update()"
            >修改</el-button
          >
        </template>
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('basic_information') }}</p>
          </div>
          <div class="model-panel-body">
            <!-- form -->
            <basic-information
              :info="basicInfo.info"
              :label-width="basicInfo.labelWidth"
            />
          </div>
        </div>

        <div class="model-panel">
          <div class="model-panel-title">
            <Tooltip :text="$t('detailInfo')" />
          </div>
          <div class="model-panel-body">
            <div class="flex_full_item">
              <!-- 表格 -->
              <page-table
                :refresh="tableInfo.refresh"
                :init-curpage="tableInfo.initCurpage"
                v-model:data="tableInfo.data"
                :pager="false"
                :summary="false"
                :check-box="false"
                :tab-index="true"
                :summary-field-list="tableInfo.summaryFieldList"
                :field-list="tableInfo.fieldList"
                :handle="tableInfo.handle"
                :isDynamic="true"
                @handleEvent="handleEvent"
              >
              </page-table>
            </div>
          </div>
        </div>
      </NewFullScreen>
    </div>
    <purch-template-modify
      v-if="modifyInfo.visible"
      :id="modifyInfo.id"
      v-model:visible="modifyInfo.visible"
      :title="modifyInfo.title"
    />
  </div>
</template>

<script setup name="OtherTemplateDetail">
import {
  getOtherStoreTemplatesById,
  getOtherStoreTemplateDetailCustomizeList,
} from '@/api/stock.js'
import PurchTemplateModify from './modify'
import { getCustomizeSetting } from '@/api/customize'

const { proxy } = getCurrentInstance()
const emits = defineEmits()
const props = defineProps(['id', 'visible'])

const basicInfo = ref({
  info: {
    data: {},
    fieldList: [
      {
        label: '模板名称',
        value: 'name',
      },
      {
        label: '部门',
        value: 'deptName',
      },
      {
        label: '备注',
        value: 'remark',
        type: 'popover',
      },
    ],
  },
  labelWidth: '100px',
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  summaryFieldList: [
    {
      value: 'qty',
      formatter: proxy.$fn.qtyFormatter,
    },
  ],
  fieldList: [],
})
// 过滤相关配置
const filterInfo = ref({
  query: {
    customizeUserVO: {},
    queryMap: {
      orderId: {
        queryType: 'EQUALS',
        queryCategory: 'AND',
        value: props.id,
      },
    },
    sortString: 'product_id desc',
  },
})
const modifyInfo = ref({
  visible: false,
  title: '',
  id: '',
})

const showDialog = computed({
  get() {
    return props.visible
  },
  set(newValue) {
    emits('update:visible', newValue) // 更新父组件visible
  },
})

watch(
  () => modifyInfo.value.visible,
  (value) => {
    if (!value) {
      getMain()
    }
  }
)

setData()
function setData() {
  let obj = {
    price: {
      label: '单价',
      value: 'price',
      type: 'column',
      formatter: proxy.$fn.TripartiteMethod,
      permission: !proxy.$permission('otherTemplate:price'),
      hidden: true,
    },
    sumAmount: {
      label: '总金额',
      value: 'sumAmount',
      type: 'column',
      formatter: proxy.$fn.TripartiteMethod,
      permission: !proxy.$permission('otherTemplate:price'),
      hidden: true,
    },
    defaultSupplierPrice: {
      label: '默认供应商价格',
      value: 'defaultSupplierPrice',
      type: 'column',
      componentType: 'price',
      permission: !proxy.$permission('otherTemplate:price'),
      formatter: proxy.$fn.TripartiteMethod,
      hidden: true,
      show: false,
    },
  }
  tableInfo.value.fieldList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...proxy.$fn.deepClone(obj),
  })
  filterInfo.value.searchList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...proxy.$fn.deepClone(obj),
  })
  getCustomize()
}
function returnBack() {
  emits('returnBack', false)
}
// 获取列表
function getList() {
  getOtherStoreTemplateDetailCustomizeList(filterInfo.value.query).then(
    (resp) => {
      console.log(resp)
      if (resp && resp.success) {
        tableInfo.value.data = resp.data
      }
    }
  )
}
function getMain() {
  getOtherStoreTemplatesById(props.id).then((resp) => {
    basicInfo.value.info.data = resp.data
    getList()
  })
}
// 触发事件
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.value.selections = [...data]
      break
  }
}
//修改
function update() {
  modifyInfo.value.title = '修改模板'
  modifyInfo.value.id = props.id
  modifyInfo.value.visible = true
}
function getCustomize() {
  let _tableInfo = tableInfo.value
  let param = {
    tableKey:
      'stockOtherStoreTemplateDetail-com.slip.spd.entity.vo.stock.OtherStoreTemplateDetailVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      filterInfo.value.query.customizeUserVO = res.data
      _tableInfo.fieldList = proxy.$fn.setFieldList(
        _tableInfo.fieldList,
        data.detail
      )
      getMain()
    }
  })
}
</script>

<style scoped></style>
